//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "SeqResRecord.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHTEXT 100
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
SeqResRecord::SeqResRecord (): serNum(0),
                               chainID('\0'),
                               numRes(0) {
  
  this->resName  = new TListE <String> ();
  this->toString = new String();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
SeqResRecord::SeqResRecord (const SeqResRecord& seqResRecord) {
  
  this->serNum     = seqResRecord.serNum;
  this->chainID    = seqResRecord.chainID;
  this->numRes     = seqResRecord.numRes;
  
  this->resName  = new TListE <String> (seqResRecord.resName);
  this->toString = new String();
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
SeqResRecord::SeqResRecord (const SeqResRecord* seqResRecord) {
  
  this->serNum     = seqResRecord->serNum;
  this->chainID    = seqResRecord->chainID;
  this->numRes     = seqResRecord->numRes;
  
  this->resName  = new TListE <String> (seqResRecord->resName);
  this->toString = new String();
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
SeqResRecord::~SeqResRecord () {
  
  if (this->resName)  delete this->resName;
  if (this->toString) delete this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void SeqResRecord::SerNum (int serNum) {
  
  this->serNum = serNum;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void SeqResRecord::ChainID (char chainID) {
  
  this->chainID = chainID;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void SeqResRecord::NumRes (int numRes) {
  
  this->numRes = numRes;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void SeqResRecord::ResName (TListE <String>* resName) {
  
  if (resName) *(this->resName) = *(resName);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int SeqResRecord::SerNum (void) {
  
  return this->serNum;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
char SeqResRecord::ChainID (void) {
  
  return this->chainID;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int SeqResRecord::NumRes (void) {
  
  return this->numRes;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <String>* SeqResRecord::ResName (void) {
  
  return this->resName;
  
} 
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool SeqResRecord::ReadFromString (String* data) {
  
  if (!data || !data->Length()) return false;
  
  String* tmp = NULL;
  
  tmp = data->SubStr(7, 10);
  if (tmp) { this->serNum = atoi(tmp->Out()); delete tmp; }
  else return false;
  
  tmp = data->SubStr(11, 12);
  if (tmp) { this->chainID = tmp->Out()[0];   delete tmp; }
  else return false;

  tmp = data->SubStr(13, 17);
  if (tmp) { this->numRes = atoi(tmp->Out()); delete tmp; }
  else return false;
  
  for (int i = 0; i < 13; i++) {
    tmp = data->SubStr(19 + (i*4), 22 + (i*4));
    if (tmp && strlen(tmp->Out()) && strcmp(tmp->Out(), "   ") != 0) { 
      this->resName->Add(new String (tmp)); 
      delete tmp; 
    }
  }
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool SeqResRecord::ReadFromString (char* data) {
  
  if (!data || !strlen(data)) return false;
  
  String dataChar;
  dataChar.In(data);
  
  return this->ReadFromString(&dataChar);    

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void SeqResRecord::CleanData (void) {
  
  this->resName->Clear();
  
  this->serNum  = 0;
  this->chainID = '\0';
  this->numRes  = 0;
 
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* SeqResRecord::ToString (void) {
  
  char    buffer [LENGTHTEXT + 1];

  sprintf(buffer, "SEQRES  %3d %c %4d\0", this->serNum, this->chainID, this->numRes);
  
  for (int i = 0; i < 13; i++)
    if (i < this->resName->Length())
      sprintf(buffer, "%s %3s\0", buffer, this->resName->Get(i)->Out());
    else break;
  
  this->toString->In(buffer);
  
  return this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* SeqResRecord::ToStringSequence (void) {

  this->toString->In("");
  
  for (int i = 0; i < 13; i++)
    if (i < this->resName->Length()) {
      if      (!strcmp(this->resName->Get(i)->Out(), ALA3)) this->toString->Add(ALA1);
      else if (!strcmp(this->resName->Get(i)->Out(), ARG3)) this->toString->Add(ARG1);
      else if (!strcmp(this->resName->Get(i)->Out(), ASN3)) this->toString->Add(ASN1);
      else if (!strcmp(this->resName->Get(i)->Out(), ASP3)) this->toString->Add(ASP1);
      else if (!strcmp(this->resName->Get(i)->Out(), ASX3)) this->toString->Add(ASX1);
      else if (!strcmp(this->resName->Get(i)->Out(), CYS3)) this->toString->Add(CYS1);
      else if (!strcmp(this->resName->Get(i)->Out(), GLU3)) this->toString->Add(GLU1);
      else if (!strcmp(this->resName->Get(i)->Out(), GLN3)) this->toString->Add(GLN1);
      else if (!strcmp(this->resName->Get(i)->Out(), GLX3)) this->toString->Add(GLX1);
      else if (!strcmp(this->resName->Get(i)->Out(), GLY3)) this->toString->Add(GLY1);
      else if (!strcmp(this->resName->Get(i)->Out(), HIS3)) this->toString->Add(HIS1);
      else if (!strcmp(this->resName->Get(i)->Out(), ILE3)) this->toString->Add(ILE1);
      else if (!strcmp(this->resName->Get(i)->Out(), LEU3)) this->toString->Add(LEU1);
      else if (!strcmp(this->resName->Get(i)->Out(), LYS3)) this->toString->Add(LYS1);
      else if (!strcmp(this->resName->Get(i)->Out(), MET3)) this->toString->Add(MET1);
      else if (!strcmp(this->resName->Get(i)->Out(), PHE3)) this->toString->Add(PHE1);
      else if (!strcmp(this->resName->Get(i)->Out(), PRO3)) this->toString->Add(PRO1);
      else if (!strcmp(this->resName->Get(i)->Out(), SER3)) this->toString->Add(SER1);
      else if (!strcmp(this->resName->Get(i)->Out(), THR3)) this->toString->Add(THR1);
      else if (!strcmp(this->resName->Get(i)->Out(), TRP3)) this->toString->Add(TRP1);
      else if (!strcmp(this->resName->Get(i)->Out(), TYR3)) this->toString->Add(TYR1);
      else if (!strcmp(this->resName->Get(i)->Out(), VAL3)) this->toString->Add(VAL1);
      else                                                  this->toString->Add(UNK1);
    }
    else break;
  
  return this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
SeqResRecord SeqResRecord::operator= (const SeqResRecord& seqResRecord) {
  
  this->serNum     = seqResRecord.serNum;
  this->chainID    = seqResRecord.chainID;
  this->numRes     = seqResRecord.numRes;

  *(this->resName) = *(seqResRecord.resName);
  
  return *this;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//

